/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.facebook.presto.plugin.neo4j.adaptive;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * A java pojo packages Neo4jMetadataTable
 */
public class NTable
        implements Serializable
{
    private static final long serialVersionUID = 20210405170433L;

    private String tableName;
    private List<NColumn> nColumnList;

    public NTable(Builder builder)
    {
        this.tableName = builder.tableName;
        this.nColumnList = builder.nColumnList;
    }

    public NTable(String tableName)
    {
        this.tableName = tableName;
        this.nColumnList = new ArrayList<>();
    }

    public List<NColumn> getnColumnList()
    {
        return nColumnList;
    }

    public String getTableName()
    {

        return tableName;
    }

    @Override
    public boolean equals(Object o)
    {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        NTable nTable = (NTable) o;
        return Objects.equals(tableName, nTable.tableName);
    }

    @Override
    public int hashCode()
    {

        return Objects.hash(tableName);
    }

    @Override
    public String toString()
    {
        return "NTable{" +
                "tableName='" + tableName + '\'' +
                ", nColumnList=" + nColumnList +
                '}';
    }

    /**
     * A Builder to create NTable.
     * Example:
     * NTable nTable = new NTable.Builder("table")
     * .addColumn("c1", 1, "1")
     * .addColumn("c2", 2, "2")
     * .build();
     */
    public static class Builder
    {
        private String tableName;
        private List<NColumn> nColumnList = new ArrayList<>();

        public Builder(String tableName)
        {
            this.tableName = tableName;
        }

        public Builder addColumn(String columnName, int columnType, String parameters)
        {
            nColumnList.add(new NColumn(columnName, columnType, parameters));
            return this;
        }

        public Builder addColumn(String columnName, int columnType)
        {
            nColumnList.add(new NColumn(columnName, columnType, null));
            return this;
        }

        public NTable build()
        {
            if (nColumnList.size() == 0) {
                nColumnList.add(new NColumn());
            }
            return new NTable(this);
        }
    }
}

